<?php
/**
 * 管理员控制器
 * Date: 2025/08/31
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Admin\AdminService;
use Illuminate\Http\Request;

class AdminController extends BaseController
{
    /**
     * 管理员列表
     * @date 2025/09/08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function list(Request $request)
    {
        $params = $this->validate($request, [
            'account' => 'sometimes',
            'status' => 'sometimes',
            'page' => 'sometimes',
            'size' => 'sometimes'
        ]);
        return (new AdminService())->list($params);
    }

    /**
     * 管理员创建
     * @date 2025/09/08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function create(Request $request)
    {
        $params = $this->validate($request, [
            'account' => 'required',
            'password' => 'required',
            'avatar' => 'sometimes',
            'name' => 'sometimes',
            'email' => 'sometimes',
            'phone' => 'sometimes',
            'status' => 'required|in:1,2',
        ], [
            'account.required' => '账号不能为空',
            'password.required' => '密码不能为空',
            'status.required' => '状态不能为空',
            'status.in' => '状态必须为开启或者禁用'
        ]);
        return (new AdminService())->create($params);
    }

    /**
     * 管理员详情
     * @date 2025/09/08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id)) {
            return $this->failed('管理员ID不能为空');
        }
        return (new AdminService())->detail($id);
    }

    /**
     * 管理员更新
     * @date 2025/09/08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required|integer',
            'avatar' => 'sometimes',
            'name' => 'sometimes',
            'email' => 'sometimes',
            'phone' => 'sometimes',
            'status' => 'required|in:1,2',
        ], [
            'id.required' => '管理员ID不能为空',
            'id.integer' => '管理员ID必须为数字类型',
            'status.required' => '状态不能为空',
            'status.in' => '状态必须为开启或者禁用'
        ]);
        return (new AdminService())->update($params);
    }

    /**
     * 管理员删除
     * @date 2025/09/08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id)) {
            return $this->failed('管理员ID不能为空');
        }
        return (new AdminService())->delete($id);
    }

    /**
     * 管理员重置密码
     * @date 2025/09/08
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function reset(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id)) {
            return $this->failed('管理员ID不能为空');
        }
        return (new AdminService())->reset($id);
    }
}
